package com.gujiangbo.application.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateTool {

	private static Log debugLog = LogFactory.getLog(DateTool.class);
	public static final String PATTERN_6 = "yyyyMM";
	public static final String PATTERN_8 = "yyyyMMdd";
	public static final String PATTERN_10 = "yyyy-MM-dd";
	public static final String PATTERN_14 = "yyyyMMddHHmmss";
	public static final String PATTERN_17 = "yyyyMMddHHmmssSSS";
	public static final String PATTERN_19 = "yyyy-MM-dd HH:mm:ss";
	public static final String PATTERN_23 = "yyyy-MM-dd HH:mm:ss SSS";
	public static final String PATTERN_24 = "yyyy-MM-dd'T'HH:mm:ss.SSS";

	public static String getCurrentTime() {
		return getCurrentTime("yyyyMMddHHmmss");
	}

	public static String get17CurrentTime() {
		return getCurrentTime("yyyyMMddHHmmssSSS");
	}

	public static String get24CurrentTime() {
		return getCurrentTime("yyyy-MM-dd'T'HH:mm:ss.SSS");
	}

	public static String getYesterday() {
		Calendar cal = Calendar.getInstance();
		cal.add(5, -1);
		String yesterday = new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
		return yesterday;
	}

	public static String getYesterday14CurrentTime() {
		Calendar cal = Calendar.getInstance();
		cal.add(5, -1);
		String yesterday = new SimpleDateFormat("yyyyMMddHHmmss").format(cal.getTime());
		return yesterday;
	}

	public static String getToday() {
		return getCurrentTime("yyyyMMdd");
	}

	public static String getCurrentHour() {
		return getCurrentTime("yyyyMMddHH");
	}

	public static String getCurrentMinute() {
		return getCurrentTime("yyyyMMddHHmm");
	}

	public static String getCurrentTime(String pattern) {
		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();
		return formatDate(date, pattern);
	}

	public static String changeDate(String date) {
		if (StringUtils.isBlank(date)) {
			return date;
		}
		return changeDate(date, "yyyyMMddHHmmss");
	}

	public static String changeDate(String date, String pattern) {
		String str_dateNew = null;
		if ((date != null) && (pattern != null)) {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			Date str_date = null;
			try {
				str_date = format.parse(date);
				SimpleDateFormat sdfl = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
				str_dateNew = sdfl.format(str_date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return str_dateNew;
	}

	public static String conversionDate(String date) {
		if (StringUtils.isBlank(date)) {
			return date;
		}
		return conversionDate(date, "yyyyMMddHHmmss");
	}

	public static String conversionDate(String date, String pattern) {
		String str_dateNew = null;
		if ((date != null) && (pattern != null)) {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			Date str_date = null;
			try {
				str_date = format.parse(date);
				SimpleDateFormat sdfl = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				str_dateNew = sdfl.format(str_date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return str_dateNew;
	}

	public static String formatDate(Date date) {
		return formatDate(date, "yyyyMMddHHmmss");
	}

	public static String formatDate(Date date, String pattern) {
		String str_date = null;
		if ((date != null) && (pattern != null)) {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			str_date = format.format(date);
		}
		return str_date;
	}

	public static String converDate(Date date, String pattern) {
		if ((date != null) && (StringUtils.isNotBlank(pattern))) {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			return format.format(date);
		}
		return "";
	}

	public static Date converString(String dateStr, String pattern) {
		if ((StringUtils.isNotBlank(dateStr)) && (StringUtils.isNotBlank(pattern))) {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			try {
				return format.parse(dateStr);
			} catch (ParseException e) {
				debugLog.error("DateTool.converString():" + e);
			}
		}
		return null;
	}

	public static String converString(String dateStr, String oldPattern, String newPattern) {
		return converDate(converString(dateStr, oldPattern), newPattern);
	}

	public static String addDate(String dateString, int number, int type) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			sdf = new SimpleDateFormat("yyyyMMdd");
			try {
				date = sdf.parse(dateString);
			} catch (ParseException e1) {
				return null;
			}
		}
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(date);

		calendar.add(type, number);

		return sdf.format(calendar.getTime());
	}

	public static String addMinute(String dateString, int number, int type) {
		if (StringUtils.isBlank(dateString)) {
			return dateString;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (ParseException e) {
			sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			try {
				date = sdf.parse(dateString);
			} catch (ParseException e1) {
				return null;
			}
		}
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(date);

		calendar.add(type, number);

		return sdf.format(calendar.getTime());
	}

	public static void main(String[] args) {
	}

	public static String cutTime(String dateString) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		SimpleDateFormat resultFormat = new SimpleDateFormat("yyyy-MM-dd");

		Date date = null;
		try {
			date = sdf.parse(dateString);

			return resultFormat.format(date);
		} catch (ParseException e) {
		}
		return dateString;
	}

	public static boolean validTime(String time, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			sdf.parse(time);
		} catch (ParseException e) {
			return false;
		}
		return true;
	}

	public static String formatDateStr(String dateStr) {
		if (StringUtils.isEmpty(dateStr)) {
			return null;
		}
		if (dateStr.length() == 8) {
			dateStr = dateStr + "000000";
		}
		return dateStr;
	}

	public static String getDateToNum(String date) {
		String str_time = "";
		try {
			SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmss");
			Date date1 = ft.parse("19700101080000");
			Date date2 = ft.parse(date);
			long quot = date2.getTime() - date1.getTime();
			long time = quot / 1000L;
			str_time = String.valueOf(time);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str_time;
	}

	public static String getUTCTime(Date date, boolean isMS) {
		if (date == null) {
			return null;
		}
		int msInMin = 60000;
		int minInHr = 60;

		DateFormat dateFormat = DateFormat.getDateTimeInstance(1, 1);
		if (isMS) {
			dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		} else {
			dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		}
		TimeZone zone = dateFormat.getTimeZone();
		int Minutes = zone.getOffset(date.getTime()) / 60000;
		int Hours = Minutes / 60;
		zone = TimeZone.getTimeZone("GMT Time" + (Hours >= 0 ? "+" : "") + Hours + ":" + Minutes);
		dateFormat.setTimeZone(zone);
		return dateFormat.format(date);
	}

	public static String getUTCTime(String date) {
		if (StringUtils.isBlank(date)) {
			return date;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		return getUTC(dateFormat, date, true);
	}

	public static String getUTCTimeToSSS(String date) {
		if (StringUtils.isBlank(date)) {
			return date;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return getUTC(dateFormat, date, false);
	}

	public static String getUTC(SimpleDateFormat dateFormat, String date, boolean isMs) {
		String utctime = "";
		try {
			utctime = getUTCTime(dateFormat.parse(date), isMs);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return utctime;
	}

	public static long diffTime(String startTime, String endTime, String type) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		long nd = 86400000L;
		long nh = 3600000L;
		long nm = 60000L;
		long ns = 1000L;
		long diff = 0L;
		long day = 0L;
		long hour = 0L;
		long min = 0L;
		long sec = 0L;

		diff = sdf.parse(endTime).getTime() - sdf.parse(startTime).getTime();
		System.out.println("diff=" + diff);
		if (diff < 0L) {
			return diff;
		}
		day = diff / nd;
		hour = diff % nd / nh + day * 24L;
		min = diff % nd % nh / nm + hour * 60L;
		sec = diff % nd % nh % nm / ns + min * 60L;
		if ("DAY".equals(type)) {
			return day;
		}
		if ("HOUNR".equals(type)) {
			return hour;
		}
		if ("MINUTE".equals(type)) {
			return min;
		}
		if ("SECOND".equals(type)) {
			return sec;
		}
		return 0L;
	}

	public static String getLastMonth() {
		Calendar cal = Calendar.getInstance();
		cal.add(2, -1);
		String yesterday = new SimpleDateFormat("yyyyMM").format(cal.getTime());
		return yesterday;
	}

	public static String getMonth(int monthes) {
		Calendar cal = Calendar.getInstance();
		cal.add(2, monthes);
		String yesterday = new SimpleDateFormat("yyyyMM").format(cal.getTime());
		return yesterday;
	}

	public static String getDay(int days) {
		Calendar cal = Calendar.getInstance();
		cal.add(5, days);
		String yesterday = new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
		return yesterday;
	}

	public static String getNowMonth() {
		return getCurrentTime("yyyyMM");
	}

}
